package com.yinp.proapp.module.study.date;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.yinp.proapp.R;
import com.yinp.tools.fragment_dialog.BaseDialogFragment;
import com.yinp.tools.fragment_dialog.CommonDialogFragment;
import com.yinp.tools.fragment_dialog.DialogFragmentHolder;
import com.yinp.tools.fragment_dialog.ViewConvertListener;
import com.yinp.tools.utils.DateUtils;
import com.yinp.tools.view.CustomScrollView;

import java.util.Calendar;

/**
 * @author :yinpeng
 * date      :2021/8/13
 * package   :com.example.myapplication.utils
 * describe  :
 */
public class SelectDateUtils {
    //    public int mYear;
//    public int mMonth;
//    public int mDay;
//    public int mHour;
//    public int mMinute;
    private SelectDateBean selectDateBean;

    private SelectDateBean getDateTime() {
        Calendar calendar = Calendar.getInstance();
        int mYear = calendar.get(Calendar.YEAR);
        int mMonth = calendar.get(Calendar.MONTH) + 1;
        int mDay = calendar.get(Calendar.DAY_OF_MONTH);
        int mHour = calendar.get(Calendar.HOUR_OF_DAY);
        int mMinute = calendar.get(Calendar.MINUTE);
        return new SelectDateBean(mYear, mMonth, mDay, mHour, mMinute);
    }

    private void setCurDatePosition() {
        selectDateBean.yearPosition = 0;
        selectDateBean.monthPosition = selectDateBean.mMonth - 1;
        selectDateBean.dayPosition = selectDateBean.mDay - 1;
        selectDateBean.hourPosition = selectDateBean.mHour;
        selectDateBean.minutePosition = selectDateBean.mMinute;
    }

    /**
     * 默认总是当前时间
     * isSelectCur  是否选中当前，true 一直是选中当前，false根据每次选中来决定
     * 开始时间是当前年，结束时间是十年后
     */
    public SelectDateUtils(boolean isSelectCur) {
        selectDateBean = getDateTime();
        selectDateBean.setStartYear(selectDateBean.mYear);
        selectDateBean.setEndYear(selectDateBean.mYear + 10);
        selectDateBean.isSelectCur = isSelectCur;
        setCurDatePosition();
    }

    /**
     * @param startYear 开始时间
     * @param endYear   结束时间
     */
    public SelectDateUtils(boolean isSelectCur, int startYear, int endYear) {
        if (startYear < 1940) {
            startYear = 1940;
        }
        selectDateBean = getDateTime();
        selectDateBean.setStartYear(startYear);
        selectDateBean.setEndYear(endYear);
        selectDateBean.isSelectCur = isSelectCur;
        setCurDatePosition();
        if (selectDateBean.mYear >= startYear && selectDateBean.mYear <= endYear) {
            selectDateBean.yearPosition = selectDateBean.mYear - startYear;
        }else {
            selectDateBean.yearPosition =0;
            selectDateBean.monthPosition =0;
            selectDateBean.dayPosition=0;
        }
    }

    /**
     * 提供入口时间
     *
     * @param selectDateBean
     */
    public SelectDateUtils(SelectDateBean selectDateBean) {
        this.selectDateBean = selectDateBean;
    }

    /**
     * @param activity
     * @param type            0年月，1年月日，2时分,3年月日时分
     * @param getDateListener 返回选中的时间
     */
    public void showDate(FragmentActivity activity, int type, GetDateListener getDateListener) {
        int layoutId = setLayoutId(type);
        if (layoutId == -1) {
            return;
        }
        CommonDialogFragment.newInstance().setLayoutId(layoutId).setViewConvertListener(new ViewConvertListener() {
            @Override
            public void convertView(DialogFragmentHolder holder, BaseDialogFragment dialogFragment) {
                CustomScrollView csv_year = null;
                CustomScrollView csv_month = null;
                CustomScrollView csv_day = null;
                CustomScrollView csv_hour = null;
                CustomScrollView csv_minute = null;
                switch (type) {
                    case 0:
                        csv_year = holder.getView(R.id.csv_year);
                        csv_month = holder.getView(R.id.csv_month);
                        LinearLayout ll_day = holder.getView(R.id.ll_day);
                        ll_day.setVisibility(View.GONE);
                        break;
                    case 1:
                        csv_year = holder.getView(R.id.csv_year);
                        csv_month = holder.getView(R.id.csv_month);
                        csv_day = holder.getView(R.id.csv_day);
                        break;
                    case 2:
                        csv_hour = holder.getView(R.id.csv_hour);
                        csv_minute = holder.getView(R.id.csv_minute);
                        break;
                    case 3:
                        csv_year = holder.getView(R.id.csv_year);
                        csv_month = holder.getView(R.id.csv_month);
                        csv_day = holder.getView(R.id.csv_day);
                        csv_hour = holder.getView(R.id.csv_hour);
                        csv_minute = holder.getView(R.id.csv_minute);
                        break;
                }

                TextView tv_left = holder.getView(R.id.tv_left);
                TextView tv_right = holder.getView(R.id.tv_right);
                TextView tv_title = holder.getView(R.id.tv_title);
                TextView tv_date = holder.getView(R.id.tv_date);

                tv_title.setText("时间选择");
                if (type != 2) {
                    csv_year.setDataList(DateUtils.scrollDate.setYear(selectDateBean.getStartYear(), selectDateBean.getEndYear()));//设置要显示的数组，必须是String
                    csv_year.setSelected(selectDateBean.yearPosition);//设置当前选中的位置
                    csv_month.setDataList(DateUtils.scrollDate.setMonth());
                    csv_month.setSelected(selectDateBean.monthPosition);
                    CustomScrollView finalCsv_day = csv_day;
                    csv_year.setOnSelectListener((view, selected, position) -> {
                        selectDateBean.mYear = Integer.parseInt(selected);
                        if (!selectDateBean.isSelectCur) {
                            selectDateBean.yearPosition = position;
                            selectDateBean.dayPosition = 0;
                        }
                        if (type == 1 || type == 3) {
                            selectDateBean.mDay = 1;
                            finalCsv_day.setDataList(DateUtils.scrollDate.setDay(selectDateBean.mYear, selectDateBean.mMonth));
                        }
                        initTvDate(tv_date, type);
                    });
                    csv_month.setOnSelectListener((view, selected, position) -> {
                        selectDateBean.mMonth = Integer.parseInt(selected);
                        if (!selectDateBean.isSelectCur) {
                            selectDateBean.monthPosition = position;
                            selectDateBean.dayPosition = 0;
                        }
                        if (type == 1 || type == 3) {
                            selectDateBean.mDay = 1;
                            finalCsv_day.setDataList(DateUtils.scrollDate.setDay(selectDateBean.mYear, selectDateBean.mMonth));
                        }
                        initTvDate(tv_date, type);
                    });
                }
                if (type == 1 || type == 3) {
                    csv_day.setDataList(DateUtils.scrollDate.setDay(selectDateBean.getStartYear(), selectDateBean.getEndYear()));
                    csv_day.setSelected(selectDateBean.dayPosition);
                    csv_day.setOnSelectListener((view, selected, position) -> {
                        selectDateBean.mDay = Integer.parseInt(selected);
                        if (!selectDateBean.isSelectCur) {
                            selectDateBean.dayPosition = position;
                        }
                        initTvDate(tv_date, type);
                    });
                }
                if (type == 2 || type == 3) {
                    csv_hour.setDataList(DateUtils.scrollDate.hourList);
                    csv_hour.setSelected(selectDateBean.hourPosition);
                    csv_minute.setDataList(DateUtils.scrollDate.setMinute());
                    csv_minute.setSelected(selectDateBean.minutePosition);
                    csv_hour.setOnSelectListener(new CustomScrollView.OnSelectListener() {
                        @Override
                        public void onSelect(View view, String selected, int position) {
                            selectDateBean.mHour = Integer.parseInt(selected);
                            if (!selectDateBean.isSelectCur) {
                                selectDateBean.hourPosition = position;
                            }
                            initTvDate(tv_date, type);
                        }
                    });
                    csv_minute.setOnSelectListener(new CustomScrollView.OnSelectListener() {
                        @Override
                        public void onSelect(View view, String selected, int position) {
                            selectDateBean.mMinute = Integer.parseInt(selected);
                            if (!selectDateBean.isSelectCur) {
                                selectDateBean.minutePosition = position;
                            }
                            initTvDate(tv_date, type);
                        }
                    });
                }

                initTvDate(tv_date, type);
                tv_left.setOnClickListener(v -> {
                    dialogFragment.dismiss();
                });
                tv_right.setOnClickListener(v -> {
                    dialogFragment.dismiss();
                    if (getDateListener != null) {
                        getDateListener.getDate(String.valueOf(selectDateBean.mYear), DateUtils.scrollDate.add0(selectDateBean.mMonth)
                                , DateUtils.scrollDate.add0(selectDateBean.mDay), DateUtils.scrollDate.add0(selectDateBean.mHour), DateUtils.scrollDate.add0(selectDateBean.mMinute));
                        getDateListener.getDate(selectDateBean);
                    }
                });
            }
        }).setAnimStyle(R.style.BottomDialogAnimation).setGravity(BaseDialogFragment.BOTTOM).show(activity.getSupportFragmentManager());
    }

    private void initTvDate(TextView tv_date, int type) {
        switch (type) {
            case 0:
                tv_date.setText(selectDateBean.mYear + "年" + DateUtils.scrollDate.add0(selectDateBean.mMonth) + "月");
                break;
            case 1:
                tv_date.setText(selectDateBean.mYear + "年" + DateUtils.scrollDate.add0(selectDateBean.mMonth)
                        + "月" + DateUtils.scrollDate.add0(selectDateBean.mDay) + "日");
                break;
            case 2:
                tv_date.setText(selectDateBean.mHour + "时" + selectDateBean.mMinute + "分");
                break;
            case 3:
                tv_date.setText(selectDateBean.mYear + "年" + DateUtils.scrollDate.add0(selectDateBean.mMonth) + "月" + DateUtils.scrollDate.add0(selectDateBean.mDay)
                        + "日" + DateUtils.scrollDate.add0(selectDateBean.mHour) + "时" + DateUtils.scrollDate.add0(selectDateBean.mMinute) + "分");
                break;
        }
    }

    private int setLayoutId(int type) {
        int layoutId = -1;
        switch (type) {
            case 0:
            case 1:
                layoutId = R.layout.dialog_selecte_date_ymd;
                break;
            case 2:
                layoutId = R.layout.dialog_selecte_date_hm;
                break;
            case 3:
                layoutId = R.layout.dialog_selecte_date_all;
                break;
            default:
                layoutId = -1;
        }
        return layoutId;
    }

    public abstract static class GetDateListener {
        public void getDate(String year, String month, String day, String hour, String minute) {
        }

        public void getDate(SelectDateBean selectDateBean) {
        }
    }


}
